﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using BLL.Services.Messages.Listing;

namespace BLL.Services.Messages
{
    public class MessagesService : IMessagesService
    {
        private readonly MessagesCollection _messagesCollection;

        public MessagesService(MessagesCollection messagesCollection)
        {
            _messagesCollection = messagesCollection;
        }

        public Messages GetMessegeById(int messageId)
        {
            return _messagesCollection.SelectById(messageId);
        }

        public void Create(Messages message)
        {
            try
            {
                var result = _messagesCollection.Create(message);
                message.MessageId = result.MessageId;
            }
            catch (SqlException sqlException)
            {

                throw new ServiceException(sqlException.Message);
            }
        }

        public void Save(Messages messages)
        {
            try
            {
                _messagesCollection.Save(messages);
            }
            catch (SqlException sqlException)
            {
                
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Delete(int messageId)
        {
            try
            {
                _messagesCollection.Delete(messageId);
            }
            catch (SqlException sqlException)
            {
                
                throw new ServiceException(sqlException.Message);
            }
        }

        public IEnumerable<Messages> GetAll()
        {
            return _messagesCollection.SelectAll();
        }



        public ListingUserMessage GetMesagesByUser(int page,int userId,int size,string column,int direction,ListingUserMessageFilter filter)
        {
            return _messagesCollection.GetListingMessage(page,userId,size,column,direction,filter);
        }

        public void UpdateNotReadMessage(int senderId, int recipientId, DateTime dateOfRead)
        {
            _messagesCollection.SetNotReadMessage(senderId,recipientId,dateOfRead);
        }

        public ListingMessage ListingMessages(int recepientId, int senderId, int page, int size)
        {
            return _messagesCollection.GetListingUser(recepientId,senderId,page,size);
        }
    }
}
